package com.easy.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.easy.bean.Address;
import com.easy.bean.Users;
import com.easy.exception.NoPassException;
import com.easy.exception.NoUserException;
import com.easy.user.mapper.AddressMapper;
import com.easy.user.mapper.IUsersMapper;
import com.easy.user.service.IUserService;
import com.easy.user.util.JWTUtil;
import com.easy.util.DigestutilMd5;
import com.easy.util.VerifyUtil;
@Service
public class UserServiceImpl implements IUserService{
	@Autowired
	IUsersMapper userM;

	@Autowired
	AddressMapper addressM;


	@Override
	public int insertAddress(Address address) {
		return addressM.insert(address);
	}


	@Override
	public int editAvatar(int userid, String avatar) {
		UpdateWrapper<Users> uw=new UpdateWrapper();
		uw.eq("id",userid);
		uw.set("avatar",avatar);
		return userM.update(uw);
	}

	@Override
	public List<Address> getAddressList(int userid) {
		QueryWrapper<Address> qw=new QueryWrapper();
		qw.eq("userid",userid);
		return addressM.selectList(qw);
	}

	@Override
	public int deleteAddress(int id) {
		return addressM.deleteById(id);
	}

	@Override
	public Address getAddress(int id) {
		return addressM.selectById(id);
	}

	@Override
	public int setAddressDefault(int userid, int addressid) {
		UpdateWrapper<Address> uw=new UpdateWrapper();
		//将该用户的所有的地址设为非默认
		uw.set("isdefault",0);
		uw.eq("userid",userid);
		addressM.update(uw);
		//将制定的地址设置为默认
		uw=new UpdateWrapper();
		uw.set("isdefault",1);
		uw.eq("id",addressid);
		addressM.update(uw);
		return 1;
	}

	@Override
	public String login(Users user) throws NoPassException, NoUserException{
		//通过用户名查询用户的信息
		QueryWrapper qw=new QueryWrapper<>();
		qw.eq("username", user.getUsername());
		Users user_data=userM.selectOne(qw);
		if(user_data!=null) {
			//验证密码是否正确
			if(DigestutilMd5.decode(user.getPassword(), user_data.getPassword())) {
				//签发token
				//将Userid嵌入token
				Map map=new HashMap();
				map.put("userid", user_data.getId());
				String token=JWTUtil.createToken(map);
				//之后可以嵌入redis
				return token;
			}else {
				throw new NoPassException("密码不正确");
			}
		}else {
			throw new NoUserException("账号不正确");
		}
	}


	@Override
	public Users getUsersByID(Integer id) {
		Users user=userM.selectById(id);
		return user;
	}

	@Override
	public Users editUsers(Users user) {
		userM.updateById(user);
		Users result=getUsersByID(user.getId());
		return result;
	}

	@Override
	public Users deleteUsers(Integer id) {
		Users result=getUsersByID(id);
		userM.deleteById(id);
		return result;
	}

	@Override
	public boolean editpassword(Integer id, String newpass, String oldpass, String confirmpass) throws Exception {
		//新密码和确认密码是否正确
		if(VerifyUtil.isNotEmpty(newpass,confirmpass)&&newpass.equals(confirmpass)) {
			//验证旧密码是否正确  ---之后可以嵌入redis
			Users user_data=getUsersByID(id);
			if(DigestutilMd5.decode(oldpass, user_data.getPassword())) {
				//如果正确修改
				userM.editpass(id, DigestutilMd5.randomGigest(newpass));
				return true;
			}else {
				throw new Exception("原密码不相同");
			}
			
		}else {
			//两次密码不相同
			throw new Exception("两次输入密码不相同");
		}
	}


}
